/*
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.webical.web.component.calendar;

import java.util.Arrays;
import java.util.GregorianCalendar;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.webical.util.CalendarUtils;
import org.webical.web.action.IAction;
import org.webical.web.action.WeekSelectedAction;
import org.webical.web.component.AbstractBasePanel;
import org.webical.web.component.calendar.model.EventsModel;

/**
 * Panel representing one row in the Month View
 *
 * @author Mattijs Hoitink
 *
 */
public abstract class MonthRowPanel extends AbstractBasePanel {
	private static final long serialVersionUID = 1L;

	/* Markup ID's */
	private static final String WEEK_LABEL_MARKUP_ID = "weekLabel";
	private static final String WEEK_LINK_MARKUP_ID = "weekLink";
	private static final String MONTH_DAY_REPEATER_MARKUP_ID = "monthDayRepeater";

	/** The EventsModel used */
	private EventsModel eventsModel;
	
	/** The month this range is representing */
	private int rangeMonth;
	
	private Link weekLink;
	private Label weekLabel;

	/** Contains the actions this panel can handle */
	protected  static Class[] PANELACTIONS = new Class[] { };

	/**
	 * Constructor.
	 * @param markupId The ID to use in markup
	 * @param eventsModel The EventsModel to use
	 * @param rangeMonth The month this range is representing
	 */
	public MonthRowPanel(String markupId, EventsModel eventsModel, int rangeMonth) {
		super(markupId, MonthRowPanel.class);
		this.eventsModel = eventsModel;
		this.rangeMonth = rangeMonth;
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupCommonComponents()
	 */
	public void setupCommonComponents() {
		GregorianCalendar currentDate = CalendarUtils.newTodayCalendar(eventsModel.getFirstDayOfWeek());
		currentDate.setTime(eventsModel.getStartDate());

		weekLink = new Link(WEEK_LINK_MARKUP_ID) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				GregorianCalendar weekCal = CalendarUtils.newTodayCalendar(eventsModel.getFirstDayOfWeek());
				weekCal.setTime(eventsModel.getStartDate());
				MonthRowPanel.this.onAction(new WeekSelectedAction(weekCal));
			}
		};
		weekLabel = new Label(WEEK_LABEL_MARKUP_ID, String.valueOf(currentDate.get(GregorianCalendar.WEEK_OF_YEAR)));
		weekLink.add(weekLabel);

		// Add a MonthDayRepeater to repeat the days of this week
		MonthDayRepeater monthDayRepeater = new MonthDayRepeater(MONTH_DAY_REPEATER_MARKUP_ID, eventsModel, rangeMonth) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onAction(IAction action) {
				if(Arrays.asList(MonthRowPanel.PANELACTIONS).contains(action.getClass())) {
					// Handle panel actions here
				} else {
					// Pass the action to the parent component
					MonthRowPanel.this.onAction(action);
				}
			}

		};

		add(weekLink);
		add(monthDayRepeater);
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupAccessibleComponents()
	 */
	public void setupAccessibleComponents() {
		// NOTHING TO DO
	}

	/* (non-Javadoc)
	 * @see org.webical.web.component.IAccessibilitySwitchingComponent#setupNonAccessibleComponents()
	 */
	public void setupNonAccessibleComponents() {
		// NOTHING TO DO
	}

	/**
	 * Handle actions generated by this panel
	 * @param action The action to handle
	 */
	public abstract void onAction(IAction action);

}
